nmea: Fix memory leaks (detected with reference/tracks/nmea+ms.txt).
authoroliskoli <oliskoli>
Sat, 25 Aug 2007 20:20:29 +0000 (20:20 +0000)
committeroliskoli <oliskoli>
Sat, 25 Aug 2007 20:20:29 +0000 (20:20 +0000)
nmea.c

diff --git a/nmea.c b/nmea.c
index a565da7a72a18f38f2a7cf730108d837dbb756b3..f9ea6d2efb9dbd14ebb3575ea27def5907257526 100644 (file)
--- a/nmea.c
+++ b/nmea.c
@@ -232,6 +232,17 @@ nmea_add_wpt(waypoint *wpt, route_head *trk)
        else waypt_add(wpt);
 }
 
+static void
+nmea_release_wpt(waypoint *wpt)
+{
+       if (wpt && ((wpt->Q.next == NULL) || (wpt->Q.next == &wpt->Q))) {
+               /* This waypoint isn't queued.
+                  Release it, because we don't have any reference to this
+                  waypoint (! memory leak !) */
+               waypt_free(wpt);
+       }
+}
+
 static void
 nmea_rd_init(const char *fname)
 {
@@ -389,9 +400,7 @@ gpgll_parse(char *ibuf)
        if (lngdir == 'W') lngdeg = -lngdeg;
        waypt->longitude = ddmm2degrees(lngdeg);
 
-       if (curr_waypt && (read_mode == rm_serial)) {
-               waypt_free(curr_waypt);
-       }
+       nmea_release_wpt(curr_waypt);
        curr_waypt = waypt;
 }
 
@@ -468,9 +477,7 @@ gpgga_parse(char *ibuf)
                        break;
        }
 
-       if (curr_waypt && (read_mode == rm_serial)) {
-               waypt_free(curr_waypt);
-       }
+       nmea_release_wpt(curr_waypt);
        curr_waypt = waypt;
 }
 
@@ -544,9 +551,7 @@ gprmc_parse(char *ibuf)
        if (lngdir == 'W') lngdeg = -lngdeg;
        waypt->longitude = ddmm2degrees(lngdeg);
 
-       if (curr_waypt && (read_mode == rm_serial)) {
-               waypt_free(curr_waypt);
-       }
+       nmea_release_wpt(curr_waypt);
        curr_waypt = waypt;
 }